package com.example.kotlin.mvp_simple

import android.content.res.Configuration
import android.os.Bundle

abstract class BasePresenter<out V : IMvpView<BasePresenter<V>>> : IPresenter<V> {

    override lateinit var view: @UnsafeVariance V
    //Model 手动注入

    override fun onCreate(savedInstanceState: Bundle?) = Unit
    override fun onSaveInstanceState(outState: Bundle) = Unit
    override fun onViewStateRestored(savedInstanceState: Bundle?) = Unit
    override fun onConfigurationChanged(newConfig: Configuration) = Unit
    override fun onDestroy() = Unit
    override fun onStart() = Unit
    override fun onStop() = Unit
    override fun onResume() = Unit
    override fun onPause() = Unit
    inline fun <reified T> getServiceApi(t: Class<T>): T? {
        val canonicalName = t.canonicalName
        if (model?.javaClass?.canonicalName == canonicalName) {
            return model as T
        }
        return t.newInstance().apply {
            model=this as IModel
        }
    }
//    open fun <T> getServiceApi(clazz: Class<T>): T? {
//        var obj: T? = apiMap.get(clazz.toString())
//        if (obj != null) {
//            return obj
//        }
//        synchronized(apiMap) {
//            obj = apiMap.get(clazz.toString()) as T
//            if (obj == null) {
//                obj = mRetrofit.create(clazz)
//                apiMap.put(clazz.toString(), obj)
//            }
//        }
//        return obj
//    }
}
